package ch.fhnw.qualiscan.application.pages;

import java.util.Iterator;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.RoleModel;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.pages.tabs.EditSurveyTab1Coordinator;
import ch.fhnw.qualiscan.application.pages.tabs.EditSurveyTab2Coordinator;
import ch.fhnw.qualiscan.application.pages.tabs.EditSurveyTab3Coordinator;
import ch.fhnw.qualiscan.application.pages.tabs.EditSurveyTab1;
import ch.fhnw.qualiscan.core.Page;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.StageHelper;
import ch.fhnw.qualiscan.core.libraries.NotificationLib;

import com.vaadin.ui.Component;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class EditSurvey extends Page {
	
	public Survey currentSurvey;
	public SurveyModel surveyModel;
	public DomainModel domainModel;
	public UserModel userModel;
	public RoleModel roleModel;
	public SurveyUserRole sur;
	public User user;
	public StageHelper stageHelper;
	
	private int currentTab;
	private boolean siteLoaded = false;
	
	public Tab tab1;
	public Tab tab2;
	public Tab tab3;
	public Tab tab4;
	
	public EditSurveyTab2Coordinator TabComponent2Coordinator;
	public EditSurveyTab1Coordinator TabComponent1Coordinator;
	public EditSurveyTab3Coordinator TabComponent3Coordinator;
	public EditSurveyTab1 EditSurveyTab1;
	
	@Override
	public void initModelsAndHelpers() {
		user = new User();
		user.setId(userSession.getId());
		surveyModel = new SurveyModel();
		domainModel = new DomainModel();
		userModel = new UserModel();
		stageHelper = new StageHelper();
		roleModel = new RoleModel();
	}
	
	@Override
	public void buildLayout() {
		//third parameter changed workaround
		removeAllComponents();
		tabSheet.removeAllComponents();

		
		VerticalLayout editSurveyWrapper = new VerticalLayout();
		editSurveyWrapper.setSpacing(true);
		
		editSurveyWrapper.addComponent(textHelper.h1(TM.get("editsurvey.title", currentSurvey.getTitle())));
				
				if(sur.getRole().getId() == Config.COORDINATOR_ID) {
					TabComponent1Coordinator = new EditSurveyTab1Coordinator(this);
					tab1 = tabSheet.addTab(TabComponent1Coordinator, TM.get("editsurvey.tab1.title"), null);
					
					EditSurveyTab1 = new EditSurveyTab1(this);
					tab4 = tabSheet.addTab(EditSurveyTab1, TM.get("editsurvey.tab4.title"), null);
					
					TabComponent2Coordinator = new EditSurveyTab2Coordinator(this);
					tab2 = tabSheet.addTab(TabComponent2Coordinator, TM.get("editsurvey.tab2.title"), null);
					
					TabComponent3Coordinator = new EditSurveyTab3Coordinator(this);
					tab3 = tabSheet.addTab(TabComponent3Coordinator, TM.get("editsurvey.tab3.title"), null);
					
					if (currentSurvey.getStage().getId() != Config.STAGE_SURVEY) {
						tab2.setEnabled(false);
						tab3.setEnabled(false);
						tab4.setEnabled(false);
					}
					
				}
				else {
					EditSurveyTab1 = new EditSurveyTab1(this);
					tab1 = tabSheet.addTab(EditSurveyTab1, TM.get("editsurvey.tab4.title"), null);
				}
			editSurveyWrapper.addComponent(tabSheet);
			addComponent(editSurveyWrapper);
	}
	

	@Override
	public void setNeededPermissions() {
		neededPermissions.add("loggedIn");	
	}

	@Override
	public void initComponents() {
		
	}
	
	//Override initBuildLayout; buildLayout is called by thirdParameterChanged
	@Override
	public void initBuildLayout() {
	}
	
	@Override
	public void changeTab(int tab) {

		changeTab = true;
		currentTab = tab;
		
		if (siteLoaded == true) {
			Iterator<Component> iterator = tabSheet.getComponentIterator();
			int i = 0;
			
			while (iterator.hasNext()) {
				Component c = (Component) iterator.next();
				
				if(tab == i) {
					tabSheet.setSelectedTab(c);
				}
				i++;
			}
		}
	}
	
	

	@Override
	public void thirdParameterChanged(String thirdParameter) {
		
		int surId;
		
		try {
			surId = Integer.valueOf(thirdParameter).intValue();
		} catch(NumberFormatException ex) {
			NotificationLib.showError(TM.get("error"), TM.get("survey.error.exists"));
			return;
		}
		sur = surveyModel.getSurById(surId);
		if (sur == null) {
			urlLib.setPage("PermissionDenied");
		}
		currentSurvey = sur.getSurvey();
		if (currentSurvey == null) {
			urlLib.setPage("Dashboard");
		} else if(sur.getUser().getId() != user.getId()) {
			urlLib.setPage("PermissionDenied");
		} else {
			buildLayout();
			
			if (siteLoaded == false) {
				Iterator<Component> iterator = tabSheet.getComponentIterator();
				int i = 0;
				
				while (iterator.hasNext()) {
					Component c = (Component) iterator.next();
					
					if(currentTab == i) {
						tabSheet.setSelectedTab(c);
					}
					i++;
				}
			}
			siteLoaded = true;
		}

	}

}
